package com.karson.front.consumer;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.karson.common.config.rabbitmq.RabbitConfig;
import com.karson.common.enums.StatisticType;
import com.karson.common.request.UserActionRequest;
import com.karson.service.service.impl.StatisticFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import com.karson.common.dto.rabbitmq.StatisticMessageDTO;
import com.karson.service.service.IStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 *  用户行为统计消费者
 * @author ZKS
 */
@Component
public class StatisticConsumer {

    @Autowired
    private StatisticFactory factory;

    @RabbitListener(queues = RabbitConfig.ACTION_QUEUE)
    public void handleStat(String message) {
        JSON json = JSONUtil.parse(message);
        StatisticMessageDTO messageDTO = json.toBean(StatisticMessageDTO.class);
        //message 包含 type(操作类型)、实体id entityType（业务类型）
        IStatisticService strategy = factory.getStrategy(StatisticType.fromCode(messageDTO.getType()));
        strategy.changeCount(messageDTO.getMessageId(), messageDTO.getDelta(),messageDTO.getEntityType());
    }
}